package com.hst.capacity.application.biz.impl;

import com.hst.capacity.application.biz.RegionBiz;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.entity.franchisee.FranchiseeEntity;
import com.hst.capacity.domain.model.entity.setting.line.CityLineScopeEntity;
import com.hst.capacity.domain.model.entity.setting.station.ApplyStationEntity;
import com.hst.capacity.domain.model.entity.setting.station.StationEntity;
import com.hst.capacity.domain.model.entity.setting.station.SysRegionEntity;
import com.hst.capacity.domain.model.request.franchisee.FranchiseeSaveParam;
import com.hst.capacity.domain.model.response.franchisee.FranchiseRegionCodeVO;
import com.hst.capacity.domain.model.response.franchisee.FranchiseeSelectVO;
import com.hst.capacity.domain.model.response.setting.AirportVO;
import com.hst.capacity.domain.model.response.setting.BusStationVO;
import com.hst.capacity.domain.model.response.setting.RailStationVO;
import com.hst.capacity.domain.model.response.setting.station.CityVO;
import com.hst.capacity.domain.model.response.setting.station.DistrictVO;
import com.hst.capacity.domain.model.response.setting.station.ProvinceVO;
import com.hst.capacity.domain.model.response.setting.station.SysRegionVO;
import com.hst.capacity.infra.convertor.FranchiseeConvertor;
import com.hst.capacity.infra.persistence.jpa.franchisee.FranchiseRepository;
import com.hst.capacity.infra.persistence.jpa.setting.line.CityLineScopeRepository;
import com.hst.capacity.infra.persistence.jpa.setting.station.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionBizImpl implements RegionBiz {

    @Autowired
    private FranchiseRepository franchiseRepository;

    @Autowired
    private StationRepository bseStationRepository;

    @Autowired
    private ApplyStationRepository applyStationRepository;

    @Autowired
    private SysRegionRepository sysRegionRepository;

    @Autowired
    private RailStationRepository railStationRepository;

    @Autowired
    private BusStationRepository busStationRepository;

    @Autowired
    private AirportRepository airportRepository;
    @Autowired
    CityLineScopeRepository cityLineScopeRepository;

    /**
     * 获取运营商运营城市Code
     * @param idFranchisee
     * @return
     */
    @Override
    public List<FranchiseRegionCodeVO> findRegionCodeByIdFranchisee(String idFranchisee) {
        return franchiseRepository.findByIdFranchisee(idFranchisee);
    }


    /**
     * 获取城市的市内车站列表
     * @param code
     * @return
     */
    @Override
    public List<StationEntity> getInnercityStationByCode(String code) {
        return bseStationRepository.getInnercityStationByCode(code);
    }

    /**
     * 获取城市的城际车站列表
     * @param code
     * @return
     */
    @Override
    public List<StationEntity> getIntercityStationByCode(String code) {
        return bseStationRepository.findIntercityStationByCode(code);
    }

    /**
     * 获取城市的机场列表
     * @param code
     * @return
     */
    @Override
    public List<AirportVO> getAirportByCityCode(String code) {
        return airportRepository.findByCode(code);
    }

    /**
     * 获取城市的火车站列表
     * @param code
     * @return
     */
    @Override
    public List<RailStationVO> getRailStationByCityCode(String code) {
        return railStationRepository.findByCode(code);
    }

    /**
     * 获取城市的汽车站列表
     * @param code
     * @return
     */
    @Override
    public List<BusStationVO> getBusStationByCityCode(String code) {
        return busStationRepository.findByCode(code);
    }

    @Override
    public List<CityLineScopeEntity> getCityLineScopeList(String cityCode) {
        List<CityLineScopeEntity> scopeLst = cityLineScopeRepository.findByCityCode(cityCode);
        return scopeLst;
    }

    /**
     * 查看运营商资料
     * @param idFranchisee
     * @return
     */
    @Override
    public FranchiseeSelectVO select(String idFranchisee) {
        return FranchiseeConvertor.INSTANCE.convert(franchiseRepository.findById(idFranchisee).get());
    }

    @Override
    public String save(FranchiseeSaveParam param) {
        FranchiseeEntity entity;
        entity = franchiseRepository.findById(param.getIdFranchisee()).orElse(null);
        if (entity==null)
            throw new BusinessException(5002,"运营商不存在请检查");
        FranchiseeConvertor.INSTANCE.convert(entity,param);
        return franchiseRepository.save(entity).getIdFranchisee();
    }

    @Override
    public SysRegionVO getRegionListByCityCode(String cityCode) {
        SysRegionVO vo = new SysRegionVO();
        vo.setCityList(sysRegionRepository.findInServiceCityList());
        vo.setProvinceList(sysRegionRepository.findInServiceProvinceList());
        vo.setDistrictList(sysRegionRepository.findInServiceDistrictList());
        return vo;
    }

    @Override
    public List<SysRegionEntity> findRegionByCityCode(String cityCode) {
        return sysRegionRepository.findRegionByCityCode(cityCode);
    }

    @Override
    public List<ProvinceVO> getAllRegionList() {
        List<ProvinceVO> listProvince = sysRegionRepository.findProvinceList();

        for (ProvinceVO province:listProvince){
            List<CityVO> listCity = sysRegionRepository.findCityListByParentId(province.getCode());
            province.setChildren(listCity);
            for (CityVO city:listCity){

                List<DistrictVO> listDistrict = sysRegionRepository.findDistrictListByParentId(city.getCode());
                city.setChildren(listDistrict);
            }
        }
        return listProvince;
    }

    @Override
    public List<ProvinceVO> getRegionList(String citycode) {
        List<ProvinceVO> listProvince = sysRegionRepository.findProvinceList();

        for (ProvinceVO province:listProvince){
            List<CityVO> listCity = sysRegionRepository.findCityListByParentId(province.getCode());
            province.setChildren(listCity);
            for (CityVO city:listCity){

                List<DistrictVO> listDistrict = sysRegionRepository.findDistrictListByParentId(city.getCode());
                city.setChildren(listDistrict);
            }
        }
        return listProvince;
    }

    /**
     * 获取城市的市内车站列表
     * @param stationRegionCode
     * @return
     */
    @Override
    public List<StationEntity> getInnerCityStationByCode(String stationRegionCode,String regionCode) {
        List<StationEntity> list = bseStationRepository.getInnercityStationByCode(stationRegionCode);
        if (list.size()==0){
            list = bseStationRepository.getInnercityStationByCode(regionCode);
        }
        return list;
    }

    /**
     * 获取城市的城际车站列表
     * @param stationRegionCode
     * @return
     */
    @Override
    public List<StationEntity> getInterCityStationByCode(String stationRegionCode,String regionCode) {
        List<StationEntity> list = bseStationRepository.findIntercityStationByCode(stationRegionCode);
        if (list.size()==0){
            list = bseStationRepository.findIntercityStationByCode(regionCode);
        }
        return list;
    }

}
